<template>
  <!-- 还款明细报表 -->
  <section class="page repayment-record-report">
    <data-form
      :submitLoading="submitLoading"
      :model="searchModel"
      @handleResetForm="handleResetForm"
      @onSearch="refreshData"
      :page="pageService"
    >
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="searchModel.caseNumber" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="组织机构" prop="deptCodes">
          <!-- <el-cascader v-model="searchModel.deptCodes" :options="departmentData" :props="{value:'deptCode',label:'deptName'}" @change="changeDept" change-on-select :show-all-levels="false" clearable></el-cascader> -->
          <select-department
            :specialOptions="specialOptions"
            v-model="searchModel.deptCodes"
            @on-select-change="(id, code) => departemntChange(id, code)"
          ></select-department>
        </el-form-item>
        <el-form-item label="当前催收员" prop="collectorId">
          <el-select v-model="searchModel.collectorId" placeholder="请选择" clearable>
            <el-option
              v-for="item in userDataSet"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="案件类型" prop="caseType">
          <el-input v-model="searchModel.caseType" placeholder="请输入" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="案件类型" prop="caseType">
          <el-select v-model="searchModel.caseType" placeholder="请选择" clearable>
            <el-option
              v-for="{index,name,code} in $dict.getDictData('CaseType')"
              :key="index"
              :label="name"
              :value="code.replace(/M|\+/, '')"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="searchModel.sysCode" placeholder="请选择" clearable>
            <el-option
              v-for="{index,name,code} in $dict.getDictData('SysCode')"
              :key="index"
              :label="name"
              :value="code.replace(/M|\+/, '')"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="还款时间" prop="repaymentTime">
          <el-date-picker
            type="daterange"
            :editable="false"
            v-model="searchModel.repaymentTime"
            placeholder="选择日期时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </template>
    </data-form>

    <data-box
      :data="dataSet"
      @onPageChange="queryRepaymentRecord"
      :page="pageService"
      :selectionList.sync="selectionList"
    >
      <template slot="buttons">
        <el-button @click="exportCaseInfo" v-auth="2009">导出</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column
          prop="caseNumber"
          label="借据号"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="personalName"
          label="客户姓名"
          :min-width="$helper.getColumnWidth(5)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="idCard"
          label="身份证号码"
          :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="caseType"
          label="案件类型"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="sysCode"
          label="系统来源"
          :formatter="(row)=>$filter.encryptSysCode(row.sysCode)"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="collectorName"
          label="催收员"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="overdueAmount"
          label="欠款总金额"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="repayAmount"
          label="实际还款金额"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="entryTime"
          label="还款时间"
          :formatter="(row) => $filter.dateFormat(row.entryTime)"
          :min-width="$helper.getColumnWidth(7)"
          show-overflow-tooltip
        ></el-table-column>
      </template>
      <p slot="footer">{{`查询结果统计：当前还款总金额 ${$filter.toThousands(totalInfo.totalAmount)}元`}}</p>
    </data-box>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import { SortService } from "~/utils/sort.service";
import NumberRange from "~/components/common/number-range.vue";
import { SynthesizeQueryService } from "~/services/domain-service/synthesize-query.service";
import { ReportManageService } from "~/services/domain-service/report-manage.service";
import { namespace, Getter, Mutation } from "vuex-class";
import { UserService } from "~/services/management-service/user.service";
import SelectDepartment from "~/components/business-component/select-department.vue";
import { CommonService } from "~/utils/common.service";
import { DepartmentService } from "~/services/management-service/department.service";

const CollectionExecutionModule = namespace("collection-execution");

@Layout("workspace")
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    SelectDepartment
  }
})
export default class RepaymentRecordReport extends Vue {
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(ReportManageService)
  private reportManageService: ReportManageService;

  @Dependencies(SortService) private sortService: SortService;
  @Dependencies(DepartmentService) private departmentService: DepartmentService;

  @Dependencies(SynthesizeQueryService)
  private synthesizeQueryService: SynthesizeQueryService;

  @Dependencies(UserService)
  private userService: UserService;

  @Getter
  private departmentData;

  @Getter
  private isSupperAdmin;

  private totalInfo: any = {};
  private specialOptions: any[] = [];
  private submitLoading: any = true;
  private selectionList: any[] = [];

  private searchModel: any = {
    deptCodes: "",
    deptCode: "",
    collectorId: "",
    caseType: "",
    sysCode: "",
    caseNumber: "",
    repaymentTime: []
  };

  private dataSet: any[] = [];

  private userDataSet = [];

  private userModel: any = {
    deptCode: ""
  };
  private queryTotalAmount() {
    this.synthesizeQueryService
      .getTotalRepayment(this.searchModel)
      .subscribe(data => (this.totalInfo = data));
  }

  private queryRepaymentRecord() {
    this.selectionList = [];
    this.synthesizeQueryService
      .queryRepaymentRecord(this.searchModel, this.pageService)
      .subscribe(data => {
        this.dataSet = data;
        this.submitLoading = false;
      });
  }
  private handleResetForm() {
    this.searchModel.deptCode = "";
  }
  /**
   * 导出案件
   */
  private exportCaseInfo() {
    if (this.dataSet.length === 0) {
      this.$message("暂无数据可以导出");
      return;
    }
    if (
      this.selectionList.length === 0 &&
      this.searchModel.collectorId === ""
    ) {
      const request: any = {
        ...this.searchModel
      };
      this.reportManageService
        .exportRepaymentRecord(request)
        .subscribe(data => this.$common.downloadFile(data.url));
    } else {
      const selectionListId = this.selectionList
        ? this.selectionList.map(v => v.id)
        : "";
      const request: any = {
        ...this.searchModel,
        ids: selectionListId
      };
      this.reportManageService
        .exportRepaymentRecord(request)
        .subscribe(data => this.$common.downloadFile(data.url));
    }
  }

  private refreshData() {
    this.queryRepaymentRecord();
    this.queryTotalAmount();
  }

  //获取委外和电催部门数据
  private queryTelAndVisitDepartment() {
    this.departmentService.queryTelVisitOuterDepartment().subscribe(data => {
      this.specialOptions = CommonService.generateTreeData(data, {
        keyName: "id",
        parentKeyName: "parentId"
      });
    });
  }
  private departemntChange(id, code) {
    if (!code) {
      this.userDataSet = [];
    } else {
      this.searchModel.deptCode = code;
      this.getAllUserByDeptCode(code);
    }
  }

  private mounted() {
    // if(!(this.departmentData[0].deptCode)){
    //     return;
    // }
    // this.searchModel.deptCodes.push(this.departmentData[0].deptCode);
    // this.getAllUserByDeptCode();
    this.refreshData();
    this.queryTelAndVisitDepartment();
  }

  private getAllUserByDeptCode(deptCode: string) {
    //  if(this.searchModel.deptCodes.length === 0){
    //       return;
    //   }
    //  this.userModel.deptCode = this.searchModel.deptCodes[this.searchModel.deptCodes.length-1]
    //  this.userService
    //     .getAllUsers(deptCode, this.pageService)
    //     .subscribe(data => {
    //         this.userDataSet = data;
    //   });
    this.userService
      .getAllUsersList({ deptCode })
      .subscribe(data => (this.userDataSet = data))
  }
  // private changeDept(){
  //   this.getAllUserByDeptCode()
  // }
}
</script>
<style lang="less">
</style>
